/** 
 * MALL_VALID:
 * 
 * initCheck;//当页面表单需获取数据动态加载后，手动初始化验证
 * initValidItem;//当页面表单需获取数据动态加载后，手动绑定验证对象
 * checkAllGroup;//手动验证全部group
 * checkGroup;//手动验证某一group
 * validGroupItem;
 * 
 * reInit;手动初始化
 */

(function($){
	$.extend($, {
		MALL_VALID : (function(){
			
			var self = {};
			
			var validConfigList = {};//所有验证组
			var allValidItem = [];//所有验证对象
			
			var initValidItem = function(){
				validConfigList = {};
				allValidItem = [];
//				var validItemList = $('.J_valid_item');
				var submitBtn = $('.J_valid_next');
				for(var i = 0; i < submitBtn.length; i++){
					var validGroup = $(submitBtn[i]).attr('valid-group');
					var validItemGroup = $('.J_valid_item[valid-group='+ validGroup +']');
					bind1(validItemGroup);
					bind2(validItemGroup);
					bind3(validItemGroup);
				}
			};
			
			var bind1 = function(validItemGroup){
				$(validItemGroup).each(function(j,item){
					allValidItem.push(item);
					if(!validConfigList[validGroup]){
						validConfigList[validGroup] = [];
					}
					validConfigList[validGroup].push(item);
				});
			}
			var bind2 = function(validItemGroup){
				validItemGroup.on('keyup',function(){
					validItem($(this));
					var groupName = $(this).attr('valid-group');
					checkGroup(groupName);
				});
			}
			var bind3 = function(validItemGroup){
				validItemGroup.on('change blur',function(){
					validItem($(this), true);
					var groupName = $(this).attr('valid-group');
					checkGroup(groupName);
				});
			}
			var RegEx = {
				//公用方法1 用正则验证非空
				_test: function (reg, obj) {
					return RegEx.notNull.test(obj) && reg.test(obj);
				},
				//公用方法2 验证长度大于等于l
				_minLength: function(obj, l){
					return RegEx.notNull.test(obj) && obj.toString().length >= l;
				},
				
				
				/*
				validType://验证名称，valid-type
				{
					preTest:开始正则验证的条件，如身份证号大于等于18位才开始验证
					test:正则验证
				}
				*/
				notNull: {
					preTest: function () {return true},
					test: function (obj) {
						if (typeof obj !== 'undefined' && 
							obj != null && 
							obj !== '' && 
							obj !== '_null' //select 默认选项 null
							) {
							return true;
						} else {
							return false;
						}
					}
				},
				notCattle: {
					preTest: function (obj) {return true},
					test: function (obj) {
						var reg = /郑州大世界|华强北/g;
						return RegEx.notNull.test(obj) && !reg.test(obj);
					},
				},
				idCard: {
					preTest: function (obj) {
						return RegEx._minLength(obj, 18);
					},
					test: function (obj) {
						var reg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/i;
						var regDate = /^((((1[6-9]|[2-9]\d)\d{2})(0?[13578]|1[02])(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})(0?[13456789]|1[012])(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})0?2(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))0?229))$/;
						return RegEx._test(reg, obj)&&RegEx._test(regDate, obj.toString().substr(6,8));
					}
				},
				mobile: {
					preTest: function (obj) {
						return RegEx._minLength(obj, 11);
					},
					test: function (obj) {
						var isChinaMobile = /^((13[4-9])|147|(15[0-2])|(15[7-9])|178|(18[2-4])|(18[7-8]))\d{8}$/;
						//去掉所有的短横
						obj = obj.replace(/-/gi,"");
						return RegEx._test(isChinaMobile, obj);
					}
				},
				email: {
					preTest: function (obj) {
						return RegEx._minLength(obj, 5);
					},
					test: function (obj) {
						var reg = /^[\da-zA-Z_\.]+@([-\dA-Za-z]+\.)+[a-zA-Z]{2,}$/;
						return RegEx._test(reg, obj);
					}
				},
				postcode: {
					preTest: function (obj) {
						return RegEx._minLength(obj, 6);
					},
					test: function(obj){
						var reg = /^[0-9]{6}$/;
						return RegEx._test(reg, obj);
					}
				},
				//yyyy-MM-dd
				date: {
					preTest: function (obj) {
						return RegEx._minLength(obj, 10);
					},
					test: function (obj) {
						var reg = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/;
						return RegEx._test(reg, obj);
					}
				},
				userName: {
					preTest: function (obj) {
						return true;
					},
					test: function (obj) {//用户名，中文和字母组合
						var reg = /^[\u4E00-\u9FA5a-zA-Z][\u4E00-\u9FA5a-zA-Z0-9\\\-_]{5,17}$/;
						return RegEx._test(reg, obj);
					}
				},
				name: {
					preTest: function (obj) {
						return true;
					},
					test: function (obj) {//中文人名或英文人名（中间可以有"·"号）
						var reg = /^[\u4E00-\u9FA5a-zA-Z]{1,8}([\s|·][\u4E00-\u9FA5a-zA-Z]{1,8}){0,3}$/;
						return RegEx._test(reg, obj);
					}
				}
			};
			
			var validGroupItem = function(validGroupName){
				var validItemList = validConfigList[validGroupName];
				for(var i =0; i< validItemList.length; i++){
					validItem($(validItemList[i]));
				}
			}
			
			var checkGroup = function(validGroupName){
				var validItemList = validConfigList[validGroupName];
				var validThrough = true;
				var submitBtn = $('.J_valid_next[valid-group='+ validGroupName +']');
				$(validItemList).each(function(i, item){
					//var item = $(validItemList[i]);
					var validResult = $(item).attr('valid-result');
//					var validVal = $(item).val();
					validResult = validResult ? validResult : 'false';
					if('false' === validResult){
						validThrough = false;
					}
				});
				/*
				for(var i =0; i< validItemList.length; i++){
					var item = $(validItemList[i]);
					var validResult = item.attr('valid-result');
					var validVal = item.val();
					validResult = validResult ? validResult : 'false';
					if('false' == validResult){
						validThrough = false;
						submitBtn.addClass('disabled');
						break;
					}
				}
				*/
				if(validThrough){
					submitBtn.removeClass('disabled');
				}else{
					submitBtn.addClass('disabled');
				}
			}
			
			var checkAllGroup = function(){
				for(var validConfigList in validConfigList){
					if(object.hasOwnProperty(groupName)){
						checkGroup(groupName);
					}
				}
			}
			
			function validItem(item, skipPreTest){
				var type = item.attr('valid-type');//正则验证
				var validResult = true;
//				var val = item.val();
//				var valid = item.attr('id');
				if(type){
					validResult = validRegEx(item, skipPreTest);
				}
				var info = $(item.attr('valid-info'));
				if('failed' === validResult){
					info.show();
					item.attr('valid-result','false');
				}else if('passed' === validResult){
					info.hide();
					item.attr('valid-result','true');
				}else if('notReady' === validResult){
					info.hide();
					item.attr('valid-result','false');
				}
			}
			
			//正则验证，是否匹配正则表达式
			function validRegEx(item, skipPreTest){
				var type = item.attr('valid-type');
				var val = item.val();
				var regExTest = RegEx[type];
				var preTest = regExTest.preTest;//开始验证条件
				var test = regExTest.test;//正则验证
				if(skipPreTest || preTest(val)){
					if(test(val)){
						return 'passed';
					}else{
						return 'failed';
					}
				}else{
					return 'notReady';
				}
			}
			
			var initCheck = function(){
				$(allValidItem).each(function(i, item){
//					var notnull = $(allValidItem[i]).val();
//					var id = $(allValidItem[i]).attr('id');
					if(RegEx.notNull.test($(allValidItem[i]).val())){
						validItem($(allValidItem[i]));
					}
				});
				checkAllGroup();
			};
			
			self.initCheck = initCheck;
			self.initValidItem = initValidItem;
			self.checkAllGroup = checkAllGroup;
			self.checkGroup = checkGroup;
			self.validGroupItem = validGroupItem;
			self.RegEx = RegEx;
			
			self.reInit = function(){
				initValidItem();
				initCheck();
			};
			
			$(document).ready(function() {
				self.reInit();
			});
			
			return self;
		})()
	});
})(jQuery);
